const { toUrl,globalData } = getApp();
import { request } from "@/utils/request";
//{ select: true }, { select: false }, { select: false }
Page({
  toUrl,
  data: {
    globalData,
    menu: 0,
    type: "normal",
    list: [],
    page: 1,
    detailList: [],
    price: 0,
    coupon_id: "",
    order_id: "", //订单id
    isEnd:false,count:0,
    types:'',//	0-待使用 1-已使用 2-已过期
    title:''
  },
  goBack(e) {
    let { url } = e.currentTarget.dataset;
    console.log(e);
    wx.redirectTo({
      url: `${url}?type=${this.data.order_id}&coupon_list_id=${this.data.coupon_id}`,
    });
  },
  tabClick(e){
    console.log(e);
    let {index} = e.currentTarget.dataset
    this.setData({
      menu:index,
      types:index,
      page:1,
      list:[]
    })
    this.getDetail()
  },
  select(e) {
    console.log(e);
    const { index } = e.currentTarget.dataset;
    const { select } = this.data.list[index];
    this.data.list.map((item) => {
      item.select = false;
    });
    this.data.list[index].select = !select;
    console.log(this.data.list[index]);
    // if (this.data.list[index].select == false) {
    //   console.log(111);
    //   this.setData({
    //     coupon_id:''
    //   })
    // }
    this.setData({
      list: this.data.list,
      price: this.data.list[index].select == true ? this.data.list[index].money :0,
      coupon_id: this.data.list[index].select == false ? '' : this.data.list[index].coupon_id,
    });
  },
  getDetail() {
    if (this.data.page > 1 && this.data.page > this.data.count) {
      this.setData({
        isEnd: true,
      });
      return;
    }
    let data = {
      typeid:  2,
      type: this.data.types || 0, //带使用
      page: this.data.page,
    };
    wx.showLoading();
    request("coupon/mycouponlist", { data }).then((res) => {
      wx.hideLoading();
      if (res.errcode == 1) {
        let list = this.data.list;
        console.log(list);

        if (this.data.page == 1) {
          this.setData({
            list:res.data,
            count:res.data.count,
            page:++this.data.page
          });
        }else{
          res.data.map((it, idx) => {
            //   console.log(it);
            // console.log(this.data.page);
            list.push({
              coupon_id: it.coupon_id,
              name: it.name,
              money: it.money,
              end_time: it.end_time,
              status: it.status,
              select: false,
            });
          });
  
          // console.log(res.data);
          this.setData({
            list,
            count:res.data.count,
            page:++this.data.page
          });
        }
       
        console.log(9999,'------');
        if (this.data.coupon_id != '') {
          console.log(555);
          let indexs = 0
          this.data.list.map((item,index) => {
            console.log(item.coupon_id == Number(this.data.coupon_id) );
            if (item.coupon_id == Number(this.data.coupon_id)) {
            item.select = true;
            indexs = index
            }
          });
          this.setData({
            list: this.data.list,
            price: this.data.list[indexs].select == true ? this.data.list[indexs].money :0,
            coupon_id: this.data.list[indexs].coupon_id,
          });
        }
      } else {
        wx.showToast({
          title: res.message,
        });
      }
    });
  },
  onLoad(e) {
    let { type, listId,coupon_id } = e;
    console.log(e);
    if (type == "select") {
      wx.setNavigationBarTitle({
        title: "停车券",
      });
      this.setData({
        title:'选择停车券'
      })
    }else{
      wx.setNavigationBarTitle({
        title: "我的券包",
      });
      this.setData({
        types:0,
        title:'我的券包'
      })
    }
    if (type) {
      this.setData({
        type,
        order_id: listId,
        coupon_id
      });
    }
    // this.getDetail();
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady() {},

  /**
   * 生命周期函数--监听页面显示
   */
  onShow() {
    this.setData({
      page:1
    })
    this.getDetail()
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide() {},

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload() {},

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh() {},

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom() {
    let isEnd = this.data.isEnd;
    if (isEnd) {
      return;
    } else {
      this.getDetail();
    }
  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage() {},
});
